<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 扩展CodeIgniter控制器类
 */

/**
 * 基础控制器
 *
 * 前端后端控制都继承该控制器类
 */
class Base_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();

        header('Content-Type: text/html; charset=UTF-8');

        //开发模式开启调试信息
        if (defined('ENVIRONMENT') && ENVIRONMENT == 'development') $this->output->enable_profiler(true);

        //定义公共常量
        define('VERSION', '1.0');
        define('DIRECTORY_NAME', $this->router->fetch_directory());
        define('CLASS_NAME', $this->router->fetch_class());
        define('METHOD_NAME', $this->router->fetch_method());
        define('TOKEN_NAME', $this->security->get_csrf_token_name());
        define('TOKEN_VALUE', $this->security->get_csrf_hash());
        define('TIMESTAMP', time());
        define('IS_POST', $_SERVER['REQUEST_METHOD'] === 'POST' && count($_POST) ? true : false);

        //初始化数据库
        $this->load->database();

        //加载基础类库
        $this->load->library(array('Ci', 'Error'));

        //加载基础辅助函数
        $this->load->helper(array('url'));

        log_message('debug', 'Base_Controller Controller Core Class Initialized');
    }

    /**
     * 显示消息
     *
     * @param $msg string 消息
     * @param string $state 状态 'fail' or 'success'
     * @param string $referer 跳转地址
     * @param bool $refresh 是否刷新
     * @param string $template 消息模板名称
     */
    protected function show_msg($msg = '', $state = 'fail', $referer = '', $refresh = false, $template = 'msg') {
        $data = array();
        $data['referer'] = empty($referer) ? '' : site_url($referer);
        $data['refresh'] = $refresh;
        $data['state'] = $state;
        if (is_array($msg) && !empty($msg)) {
            $data['message'] = $this->lang->line($msg[0]);
            $data['data'] = $msg[1];
        } else {
            $data['message'] = $this->lang->line($msg);
            $data['data'] = '';
        }

        if ($this->input->is_ajax_request()) {
            ob_start();
            header('Content-type: application/json');
            $json = json_encode($data);
            echo $json;
        } else {
            ob_start();
            extract($data);
            include(APPPATH . "errors/{$template}.php");
        }
        $buffer = ob_get_contents();
        ob_end_clean();
        echo $buffer;
        exit;
    }
}

/**
 * 前端控制器
 *
 * 前端的所有控制器都需要继承这个类，它不包含验证
 */
class Front_Controller extends Base_Controller {

    public function __construct() {
        parent::__construct();

        //定义前端常量
        //...

        //设置模板主题
        $this->load->set_theme('front');

        log_message('debug', 'Front_Controller Controller Core Class Initialized');
    }

    /**
     * 显示错误
     *
     * @param string|array $msg
     * @param string $referer
     * @param boolean $refresh
     * @return void
     */
    protected function show_error($msg = '', $referer = '', $refresh = false) {
        $this->show_msg($msg, 'fail', $referer, $refresh);
    }

    /**
     * 显示消息
     *
     * @param string|array $msg
     * @param string $referer
     * @param boolean $refresh
     * @return void
     */
    protected function show_msg($msg = '', $referer = '', $refresh = false) {
        parent::show_msg($msg, 'success', $referer, $refresh, 'msg_front');
    }
}

/**
 * 后端控制器
 *
 * 后端的所有控制器都需要继承这个类，主要包含验证
 */
class Admin_Controller extends Base_Controller {

    protected $menus = array();

    public function __construct() {
        parent::__construct();

        //定义后端常量
        //...

        //设置模板主题
        $this->load->set_theme('admin');

        //后台菜单
        $this->load->service('admin_menu/admin_menu_service');
        $this->menus = $this->admin_menu_service->get_menus(); //var_dump($this->menus);
        $this->load->vars(array('menus' => $this->menus)); //使得模板可直接使用

        log_message('debug', 'Admin_Controller Controller Core Class Initialized');
    }

    /**
     * 显示错误
     *
     * @param string|array $msg
     * @param string $referer
     * @param boolean $refresh
     * @return void
     */
    protected function show_error($msg = '', $referer = '', $refresh = false) {
        parent::show_msg($msg, 'fail', $referer, $refresh, 'msg_admin');
    }

    /**
     * 显示消息
     *
     * @param string|array $msg
     * @param string $referer
     * @param boolean $refresh
     * @return void
     */
    protected function show_msg($msg = '', $referer = '', $refresh = false) {
        parent::show_msg($msg, 'success', $referer, $refresh, 'msg_admin');
    }
}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */
